﻿#define _CRT_SECURE_NO_WARNINGS 1


#include <iostream>
using namespace std;
#include <vector>
#include <map>

//C++98传统的{}
#if 0
struct Point
{
	int _x;
	int _y;
};

int main()
{
	int arr[] = { 1,2,3,4,5 };
	Point p = { 1, 2 };

	return 0;
}
#endif

//C++11的{}
#if 0
struct Point
{
	int _x;
	int _y;
};
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{
		cout << "Date(int year, int month, int day)" << endl;
	}
	Date(const Date& d)
		:_year(d._year)
		, _month(d._month)
		, _day(d._day)
	{
		cout << "Date(const Date& d)" << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	//内置类型可以用{}初始化
	int x1 = { 2 };
	cout << x1 << endl;

	//自定义类型也可以用{}初始化
	Date d1 = { 2025, 1, 1 };

	//这里引用的是构造出来的临时对象，const
	const Date& d2 = { 2024, 7, 25 };

	//单参数构造，可以省略{}
	Date d3 = { 2025 };//C++11
	Date d4 = 2025;//C++98

	string str = "crayon";

	//可以省略赋值
	Point p1{ 1, 2 };
	int x2{ 2 };
	Date d6{ 2024, 7, 25 };
	const Date& d7{ 2024, 7, 25 };

	//相比于匿名对象，{}更有性价比
	vector<Date> v;
	v.push_back(d1);
	v.push_back(Date(2025, 1, 1));
	v.push_back({ 2025, 1, 1 });

	map<string, string> dict;
	dict.insert(pair<string, string>("xxx", "yyy"));
	dict.insert(make_pair( "xxx", "yyy" ));
	dict.insert({ "xxx", "yyy" });

	return 0;
}
#endif

//initializer_list
#if 0
int main()
{
	//其中的元素为const
	initializer_list<int> il1 = { 12,64,87,45,98,43 };
	const vector<int>& v1 = il1;

	//隐式类型转换
	vector<int> v2 = { 32,23,67,45,65,21 };

	//传参构造
	vector<int> v3({ 32,23,67,45,65,21 });
	
	//省略赋值符号
	vector<int> v4{ 32,23,67,45,65,21 };

	//initializer_list构造+{}pair隐式类型转换
	map<string, string> dict1 = { {"xxx", "yyy"},{"hhh","aaa"} };
	map<string, string> dict2{ {"xxx", "yyy"},{"hhh","aaa"} };

	return 0;
}
#endif

//定位new
#if 0
class A
{
public:
	A(const int& a1 = 1, const int& a2 = 2)
		:_a1(a1), _a2(a2)
	{}

	//...
private:
	int _a1;
	int _a2;
};

int main()
{
	A* p = (A*)operator new(sizeof(A));
	new(p)A({ 3,5 });

	p->~A();
	
	return 0;
}
#endif

//左值与右值
#if 0
//既可以传左值，亦可以传右值
template<class T>
void func(const T& x) {}

int main()
{
	int* p = new int(0);
	int b = 1;
	const int c = b;
	*p = 10;
	string s("111111");
	s[0] = 'x';
	cout << (void*)&s[0] << endl;

	//左值引用：给左值取别名
	int*& r1 = p;
	int& r2 = b;
	const int& r3 = c;
	int& r4 = *p;
	string& r5 = s;
	char& r6 = s[0];

	double x = 1.1, y = 2.2;

	//右值引用：给右值取别名
	int&& rr1 = 10;
	int&& rr2 = x + y;
	double&& rr3 = fmin(x, y);
	string&& rr4 = string("11111");

	//左值引用不能直接引用右值，但是const左值引用可以引用右值
	const int& rx1 = 10;
	const int& rx2 = x + y;
	const double& rx3 = fmin(x, y);
	const string& rx4 = string("11111");

	//右值值引用不能直接引用左值，但是右值引用可以引用move(左值)
	int*&& rrx1 = move(p);
	int&& rrx2 = move(b);
	int&& rrx4 = move(*p);
	string&& rrx5 = move(s);
	char&& rrx6 = move(s[0]);

	string&& rrx7 = (string&&)s;
	
	//b、r1、rr1都是变量表达式，都是左值
	cout << &b << endl;
	cout << &r1 << endl;
	cout << &rr1 << endl;

	//rr1的属性是左值，不能再被右值引用绑定，除非move一下
	//int&& r7 = rr1;
	int&& r8 = move(rr1);

	return 0;
}
#endif


//引用延长生命周期
#if 0
int main()
{
	string s1 = "Test";
	//const的左值引用延⻓生存期
	const string& r2 = s1 + s1; 
	
	//右值引用延⻓生存期
	string&& r3 = s1 + s1; 
	r3 += "Test";
	cout << r3 << '\n';
	return 0;
}
#endif 

//左值、右值的参数匹配
#if 0
void f(int& x)
{
	std::cout << "左值引用重载 f(" << x << ")\n";
}
void f(const int& x)
{
	std::cout << "到const的左值引用重载 f(" << x << ")\n";
}
void f(int&& x)
{
	std::cout << "右值引用重载 f(" << x << ")\n";
}
int main()
{
	int i = 1;
	const int ci = 2;
	f(i); 
	f(ci); 
	f(3); 
	f(move(i)); 

	//右值引用变量在用于表达式时是左值
	int&& x = 1;
	f(x);
	f(move(x));

	return 0;
}
#endif

#include<cassert>
//移动构造
#if 1
namespace bit
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}
		const_iterator begin() const
		{
			return _str;
		}
		const_iterator end() const
		{
			return _str + _size;
		}
		string(const char* str = "")
			:_size(strlen(str)), _capacity(_size)
		{
			cout << "string(char* str)-构造" << endl;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}
		string(const string& s)
			:_str(nullptr)
		{
			cout << "string(const string& s) -- 拷⻉构造" << endl;
			//reserve(s._capacity);
			for (auto ch : s)
			{
				push_back(ch);
			}
		}
		//移动构造
		string(string&& s)
		{
			cout << "string(string&& s) -- 移动构造" << endl;
			swap(s);
		}
		string& operator=(const string& s)
		{
			cout << "string& operator=(const string& s) -- 拷⻉赋值" << endl;
			if (this != &s)
			{
				_str[0] = '\0';
				_size = 0;
				reserve(s._capacity);
				for (auto ch : s)
				{
					push_back(ch);
				}
			}
			return *this;
		}
		//移动赋值
		string& operator=(string&& s)
		{
			cout << "string& operator=(string&& s) -- 移动赋值" << endl;
			swap(s);
			return *this;
		}
		~string()
		{
			cout << "~string() -- 析构" << endl;
			delete[] _str;
			_str = nullptr;
		}
		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}
		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				if (_str)
				{
					strcpy(tmp, _str);
					delete[] _str;
				}
				_str = tmp;
				_capacity = n;
			}
		}
		void push_back(char ch)
		{
			if (_size >= _capacity)
			{
				size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapacity);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}
		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}
		const char* c_str() const
		{
			return _str;
		}
		size_t size() const
		{
			return _size;
		}
	private:
			char* _str = nullptr;
			size_t _size = 0;
			size_t _capacity = 0;
	};
}
#endif

//int main()
//{
//	bit::string s1("xxxxx");
//	//拷⻉构造
//	bit::string s2 = s1;
//	//构造+移动构造，优化后直接构造
//	bit::string s3 = bit::string("yyyyy");
//	//移动构造
//	bit::string s4 = move(s1);
//	cout << "******************************" << endl;
//
//	return 0;
//}

namespace bit
{
	string addStrings(string num1, string num2)
	{
		string str;
		int end1 = num1.size() - 1, end2 = num2.size() - 1;
		int next = 0;
		while (end1 >= 0 || end2 >= 0)
		{
			int val1 = end1 >= 0 ? num1[end1--] - '0' : 0;
			int val2 = end2 >= 0 ? num2[end2--] - '0' : 0;
			int ret = val1 + val2 + next;
			next = ret / 10;
			ret = ret % 10;
			str += ('0' + ret);
		}
		if (next == 1)
			str += '1';
		reverse(str.begin(), str.end());
		cout << "******************************" << endl;
	
		return str;
	}
}
//场景1
//int main()
//{
//	bit::string ret = bit::addStrings("11111", "2222");
//	cout << ret.c_str() << endl;
//
//	return 0;
//}

//场景2
//int main()
//{
//	bit::string ret;
//	ret = bit::addStrings("11111", "2222");
//	cout << ret.c_str() << endl;
//
//	return 0;
//}

int main()
{
	int a[] = { 1, 2, 3 };
	for (auto e : a)
	{
		cout << e << " ";
	}

	return 0;
}